<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  
  
  <title>Webpack基础配置(四) | Hexo</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
  <meta name="description" content="前言 问：本节有哪些loader？ url-loader、file-loader。    url-loader 安装 yarn add url-loader file-loader --dev  注：url-loader需要依赖file-loader，所以下载url-loader的同时也要下载file-loader。  在webpack.config.js中添加配置   1234567891011">
<meta property="og:type" content="article">
<meta property="og:title" content="Webpack基础配置(四)">
<meta property="og:url" content="http://example.com/2021/01/11/Webpack%E5%9F%BA%E7%A1%80%E9%85%8D%E7%BD%AE-%E5%9B%9B/index.html">
<meta property="og:site_name" content="Hexo">
<meta property="og:description" content="前言 问：本节有哪些loader？ url-loader、file-loader。    url-loader 安装 yarn add url-loader file-loader --dev  注：url-loader需要依赖file-loader，所以下载url-loader的同时也要下载file-loader。  在webpack.config.js中添加配置   1234567891011">
<meta property="og:locale" content="en_US">
<meta property="og:image" content="https://p1-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/a0a114a84d8e4cbd8c92d2806beba1fc~tplv-k3u1fbpfcp-watermark.image">
<meta property="og:image" content="https://p3-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/1b8e7e8e6ab846e9beb4d6e0d30fdd3d~tplv-k3u1fbpfcp-watermark.image">
<meta property="og:image" content="https://p3-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/60c150a5613e47cd901f170b31f42a67~tplv-k3u1fbpfcp-watermark.image">
<meta property="og:image" content="https://p9-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/df87e077643b467bbb71d14019498fba~tplv-k3u1fbpfcp-watermark.image">
<meta property="og:image" content="https://p3-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/4af38bfc13974702ae4757bf70283161~tplv-k3u1fbpfcp-watermark.image">
<meta property="og:image" content="https://p6-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/e5a4954251d448bc85329b2921912325~tplv-k3u1fbpfcp-watermark.image">
<meta property="og:image" content="https://p1-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/34e017e99f6f4aa994c7bb2d8c4cfa69~tplv-k3u1fbpfcp-watermark.image">
<meta property="article:published_time" content="2021-01-11T14:34:54.000Z">
<meta property="article:modified_time" content="2021-01-11T14:41:47.226Z">
<meta property="article:author" content="John Doe">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://p1-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/a0a114a84d8e4cbd8c92d2806beba1fc~tplv-k3u1fbpfcp-watermark.image">
  
    <link rel="alternate" href="/atom.xml" title="Hexo" type="application/atom+xml">
  
  
    <link rel="shortcut icon" href="/favicon.png">
  
  
    
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/typeface-source-code-pro@0.0.71/index.min.css">

  
  
<link rel="stylesheet" href="/css/style.css">

  
    
<link rel="stylesheet" href="/fancybox/jquery.fancybox.min.css">

  
<meta name="generator" content="Hexo 5.3.0"></head>

<body>
  <div id="container">
    <div id="wrap">
      <header id="header">
  <div id="banner"></div>
  <div id="header-outer" class="outer">
    <div id="header-title" class="inner">
      <h1 id="logo-wrap">
        <a href="/" id="logo">Hexo</a>
      </h1>
      
    </div>
    <div id="header-inner" class="inner">
      <nav id="main-nav">
        <a id="main-nav-toggle" class="nav-icon"></a>
        
          <a class="main-nav-link" href="/">Home</a>
        
          <a class="main-nav-link" href="/archives">Archives</a>
        
      </nav>
      <nav id="sub-nav">
        
          <a id="nav-rss-link" class="nav-icon" href="/atom.xml" title="RSS Feed"></a>
        
        <a id="nav-search-btn" class="nav-icon" title="Search"></a>
      </nav>
      <div id="search-form-wrap">
        <form action="//google.com/search" method="get" accept-charset="UTF-8" class="search-form"><input type="search" name="q" class="search-form-input" placeholder="Search"><button type="submit" class="search-form-submit">&#xF002;</button><input type="hidden" name="sitesearch" value="http://example.com"></form>
      </div>
    </div>
  </div>
</header>

      <div class="outer">
        <section id="main"><article id="post-Webpack基础配置-四" class="h-entry article article-type-post" itemprop="blogPost" itemscope itemtype="https://schema.org/BlogPosting">
  <div class="article-meta">
    <a href="/2021/01/11/Webpack%E5%9F%BA%E7%A1%80%E9%85%8D%E7%BD%AE-%E5%9B%9B/" class="article-date">
  <time class="dt-published" datetime="2021-01-11T14:34:54.000Z" itemprop="datePublished">2021-01-11</time>
</a>
    
  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 class="p-name article-title" itemprop="headline name">
      Webpack基础配置(四)
    </h1>
  

      </header>
    
    <div class="e-content article-entry" itemprop="articleBody">
      
        <h2 id="前言"><a href="#前言" class="headerlink" title="前言"></a>前言</h2><ul>
<li>问：本节有哪些loader？<ul>
<li>url-loader、file-loader。</li>
</ul>
</li>
</ul>
<h2 id="url-loader"><a href="#url-loader" class="headerlink" title="url-loader"></a>url-loader</h2><ol>
<li><p>安装<br> <code>yarn add url-loader file-loader --dev</code></p>
<p> 注：url-loader需要<code>依赖file-loader</code>，所以下载url-loader的同时也要下载file-loader。</p>
</li>
<li><p>在webpack.config.js中添加配置  </p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">&#123;</span><br><span class="line">	test: <span class="regexp">/\.(png|jpg|jpeg|svg|gif)$/</span>,</span><br><span class="line">	use: [</span><br><span class="line">		&#123;</span><br><span class="line">			loader: <span class="string">&#x27;url-loader&#x27;</span>,</span><br><span class="line">			options: &#123;</span><br><span class="line">				limit: <span class="number">1028</span> * <span class="number">8</span>,</span><br><span class="line">				name: <span class="string">`assets/imgs/[name].[hash:8].[ext]`</span></span><br><span class="line">			&#125;</span><br><span class="line">		&#125;</span><br><span class="line">	]</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p> <code>limit</code>：文件大小的最大值<br> <code>assets/imgs/</code>：在dist文件夹中生成的目录(目录名称随意)<br> <code>[name]</code>：文件名称<br> <code>[hash:8]</code>：8位的hash值，可用于版本控制<br> <code>[ext]</code>：后缀</p>
<p> 注：如果文件大小低于limit的值(8kb)，则返回base64的dataurl，如果文件大小高于limit的值，则内部使用file-loader根据name属性直接生成文件</p>
</li>
<li><p>测试<br> 在src文件夹下创建<br> <img src="https://p1-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/a0a114a84d8e4cbd8c92d2806beba1fc~tplv-k3u1fbpfcp-watermark.image"></p>
<p> <code>less_then_limit.png</code>为大小低于limit的文件<br> <code>more_than_limit.png</code>为大小高于limit的文件  </p>
<p> a. 使用<code>低于limit</code>的文件<br> <img src="https://p3-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/1b8e7e8e6ab846e9beb4d6e0d30fdd3d~tplv-k3u1fbpfcp-watermark.image">  </p>
<p> 执行<code>yarn build</code></p>
<p> 生成的代码效果<br> <img src="https://p3-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/60c150a5613e47cd901f170b31f42a67~tplv-k3u1fbpfcp-watermark.image">  </p>
<p> dist文件夹下<code>未生成</code>指定目录(assets/imgs/)与文件<br> <img src="https://p9-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/df87e077643b467bbb71d14019498fba~tplv-k3u1fbpfcp-watermark.image">  </p>
<p> b. 使用<code>高于limit</code>的文件<br> <img src="https://p3-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/4af38bfc13974702ae4757bf70283161~tplv-k3u1fbpfcp-watermark.image">  </p>
<p> 执行<code>yarn build</code></p>
<p> 生成的代码效果<br> <img src="https://p6-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/e5a4954251d448bc85329b2921912325~tplv-k3u1fbpfcp-watermark.image"></p>
<p> dist文件夹下<code>生成</code>指定目录(assets/imgs/)与文件<br> <img src="https://p1-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/34e017e99f6f4aa994c7bb2d8c4cfa69~tplv-k3u1fbpfcp-watermark.image"></p>
</li>
</ol>
<h2 id="file-loader"><a href="#file-loader" class="headerlink" title="file-loader"></a>file-loader</h2><p>file-loader与url-loader功能相似，只不过没有limit限制，不生成base64的dataurl，直接生成指定路径的文件  </p>
<ol>
<li><p>安装  </p>
<pre><code> `yarn add file-loader --dev`  </code></pre>
</li>
<li><p>使用<br> a. 字体文件  </p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">&#123;</span><br><span class="line">	test: <span class="regexp">/\.(woff|woff2|eot|ttf|otf)$/</span>,</span><br><span class="line">	loader: <span class="string">&#x27;file-loader&#x27;</span>,</span><br><span class="line">	options: &#123;</span><br><span class="line">		name: <span class="string">`assets/fonts/[name].[hash:8].[ext]`</span></span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p> b. 媒体文件  </p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">&#123;</span><br><span class="line">	test: <span class="regexp">/\.(mp4|ogg)$/</span>,</span><br><span class="line">	loader: <span class="string">&#x27;file-loader&#x27;</span>,</span><br><span class="line">	options: &#123;</span><br><span class="line">		name: <span class="string">`assets/media/[name].[hash:8].[ext]`</span></span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></li>
</ol>

      
    </div>
    <footer class="article-footer">
      <a data-url="http://example.com/2021/01/11/Webpack%E5%9F%BA%E7%A1%80%E9%85%8D%E7%BD%AE-%E5%9B%9B/" data-id="ckjsoehch0003fsr07vsq2btw" data-title="Webpack基础配置(四)" class="article-share-link">Share</a>
      
      
      
    </footer>
  </div>
  
    
<nav id="article-nav">
  
  
    <a href="/2020/12/20/Webpack%E5%9F%BA%E7%A1%80%E9%85%8D%E7%BD%AE-%E4%B8%89/" id="article-nav-older" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Older</strong>
      <div class="article-nav-title">Webpack基础配置(三)</div>
    </a>
  
</nav>

  
</article>


</section>
        
          <aside id="sidebar">
  
    

  
    

  
    
  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Archives</h3>
    <div class="widget">
      <ul class="archive-list"><li class="archive-list-item"><a class="archive-list-link" href="/archives/2021/01/">January 2021</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2020/12/">December 2020</a></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Recent Posts</h3>
    <div class="widget">
      <ul>
        
          <li>
            <a href="/2021/01/11/Webpack%E5%9F%BA%E7%A1%80%E9%85%8D%E7%BD%AE-%E5%9B%9B/">Webpack基础配置(四)</a>
          </li>
        
          <li>
            <a href="/2020/12/20/Webpack%E5%9F%BA%E7%A1%80%E9%85%8D%E7%BD%AE-%E4%B8%89/">Webpack基础配置(三)</a>
          </li>
        
          <li>
            <a href="/2020/12/20/Webpack%E5%9F%BA%E7%A1%80%E9%85%8D%E7%BD%AE-%E4%BA%8C/">Webpack基础配置(二)</a>
          </li>
        
          <li>
            <a href="/2020/12/20/Webpack%E5%9F%BA%E7%A1%80%E9%85%8D%E7%BD%AE-%E4%B8%80/">Webpack基础配置(一)</a>
          </li>
        
      </ul>
    </div>
  </div>

  
</aside>
        
      </div>
      <footer id="footer">
  
  <div class="outer">
    <div id="footer-info" class="inner">
      
      &copy; 2021 John Doe<br>
      Powered by <a href="https://hexo.io/" target="_blank">Hexo</a>
    </div>
  </div>
</footer>

    </div>
    <nav id="mobile-nav">
  
    <a href="/" class="mobile-nav-link">Home</a>
  
    <a href="/archives" class="mobile-nav-link">Archives</a>
  
</nav>
    


<script src="/js/jquery-3.4.1.min.js"></script>



  
<script src="/fancybox/jquery.fancybox.min.js"></script>




<script src="/js/script.js"></script>





  </div>
</body>
</html>